package com.want.evenchat.controller.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.adapter.EMAChatRoom;
import com.hyphenate.exceptions.HyphenateException;
import com.want.evenchat.R;
import com.want.evenchat.controller.adapter.InviteAdapter;
import com.want.evenchat.model.Model;
import com.want.evenchat.model.bean.InvitionInfo;
import com.want.evenchat.utils.Constant;

import java.util.List;

/**
 * Created by Want on 2018/5/7.
 */

public class InviteActivity extends Activity{
    //邀请信息列表页面

    private ListView listView;

    private InviteAdapter inviteAdapter;

    private LocalBroadcastManager mLBM;

    private BroadcastReceiver InviteChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            //刷新页面
            refresh();
        }
    };

    private InviteAdapter.OnInviteListener mOnInviteListener = new InviteAdapter.OnInviteListener() {
        @Override
        public void onAccept(final InvitionInfo invitionInfo) {
            //通知环信服务器点击接受按钮
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().contactManager().acceptInvitation(invitionInfo.getUser().getHxid());

                        //更新数据库
                        Model.getInstance().getDBManager().getInviteTableDAO().updateInvitationStatus(InvitionInfo.InvitationStatus.INVITE_ACCEPT , invitionInfo.getUser().getHxid());

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //页面发生变化
                                Toast.makeText(InviteActivity.this , "添加好友成功" , Toast.LENGTH_SHORT).show();

                                //刷新页面
                                refresh();
                            }
                        });
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //页面发生变化
                                Toast.makeText(InviteActivity.this , "添加好友失败" , Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        }

        @Override
        public void onReject(final InvitionInfo invitionInfo) {

            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().contactManager().declineInvitation(invitionInfo.getUser().getHxid());

                        //数据库变化
                        Model.getInstance().getDBManager().getInviteTableDAO().removeInvitation(invitionInfo.getUser().getHxid());

                        //界面变化
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteActivity.this , "拒绝好友申请" , Toast.LENGTH_SHORT).show();

                                refresh();
                            }
                        });

                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteActivity.this , "拒绝好友失败" , Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });

        }

        //接受邀请按钮处理
        @Override
        public void onInviteAccept(final InvitionInfo invitionInfo) {
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        //与环信服务器通信(接受了邀请)
                        EMClient.getInstance().groupManager().acceptInvitation(invitionInfo.getGroup().getGroupId() , invitionInfo.getGroup().getInvatePerson());

                        //本地数据更新
                        invitionInfo.setStatus(InvitionInfo.InvitationStatus.GROUP_ACCEPT_INVITE);
                        Model.getInstance().getDBManager().getInviteTableDAO().addInvitation(invitionInfo);

                        //内存数据的变化
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteActivity.this, "接受邀请成功", Toast.LENGTH_SHORT).show();

                                refresh();
                            }
                        });
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteActivity.this, "接受邀请失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        }

        //拒绝邀请按钮处理
        @Override
        public void onInviteReject(final InvitionInfo invitionInfo) {
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        //与环信服务器通信(拒绝了邀请)
                        EMClient.getInstance().groupManager().declineInvitation(invitionInfo.getGroup().getGroupId() , invitionInfo.getGroup().getInvatePerson() , "拒绝邀请");

                        //更新本地数据库
                        invitionInfo.setStatus(InvitionInfo.InvitationStatus.GROUP_REJECT_INVITE);
                        Model.getInstance().getDBManager().getInviteTableDAO().addInvitation(invitionInfo);

                        //更新内存的数据
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteActivity.this, "拒绝邀请", Toast.LENGTH_SHORT).show();

                                //刷新页面
                                refresh();
                            }
                        });
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        //接受申请按钮处理
        @Override
        public void onApplicationAccept(final InvitionInfo invitionInfo) {

            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        //与环信服务器通信(接受了邀请)
                        EMClient.getInstance().groupManager().acceptApplication(invitionInfo.getGroup().getGroupId() , invitionInfo.getGroup().getInvatePerson());

                        //更新数据库
                        invitionInfo.setStatus(InvitionInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION);

                        Model.getInstance().getDBManager().getInviteTableDAO().addInvitation(invitionInfo);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteActivity.this, "接受申请", Toast.LENGTH_SHORT).show();

                                //刷新
                                refresh();
                            }
                        });
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                    }
                }
            });

        }

        //拒绝申请按钮
        @Override
        public void onApplicationReject(final InvitionInfo invitionInfo) {
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        //与环信服务器通信(拒绝了申请)
                        EMClient.getInstance().groupManager().declineApplication(invitionInfo.getGroup().getGroupId(), invitionInfo.getGroup().getInvatePerson() , "拒绝申请");

                        //更新数据库
                        invitionInfo.setStatus(InvitionInfo.InvitationStatus.GROUP_REJECT_APPLICATION);
                        Model.getInstance().getDBManager().getInviteTableDAO().addInvitation(invitionInfo);

                        //更新内存
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteActivity.this, "拒绝申请", Toast.LENGTH_SHORT).show();

                                //刷新
                                refresh();
                            }
                        });

                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InviteActivity.this, "拒绝申请失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_invite);

        initView();

        initData();

    }

    private void initData() {
        //初始化ListView

        inviteAdapter = new InviteAdapter(this , mOnInviteListener);

        listView.setAdapter(inviteAdapter);

        //注册邀请信息变化的广播
        mLBM = LocalBroadcastManager.getInstance(this);
        mLBM.registerReceiver(InviteChangedReceiver, new IntentFilter(Constant.CONTACT_INVITE_CHANGED));

        mLBM.registerReceiver(InviteChangedReceiver, new IntentFilter(Constant.GROUP_INVITE_CHANGED));

        //刷新方法
        refresh();
    }



    private void initView() {
        listView = findViewById(R.id.activity_invite_listview);
    }

    private void refresh() {

        //获取数据库中的所以邀请信息
        List<InvitionInfo> invitations = Model.getInstance().getDBManager().getInviteTableDAO().getInvitations();

        //刷新适配器
        inviteAdapter.refresh(invitations);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLBM.unregisterReceiver(InviteChangedReceiver);
    }
}
